package com.bwton.msx.ccb.ui.setting;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.bwton.msx.ccb.R;
import com.bwton.msx.ccb.utils.PreferenceUtil;
import com.sensetime.ssidmobile.sdk.liveness.model.config.LivenessConfig;
import com.sensetime.ssidmobile.sdk.liveness.model.config.MotionConfig;
import com.sensetime.ssidmobile.sdk.liveness.model.config.QualityConfig;
import com.sensetime.ssidmobile.sdk.liveness.model.config.SilentConfig;

public class SettingThresholdsFragment extends Fragment {

    Switch switchHeadPoseEnable;
    Switch switchOcclusionEyeEnable;
    Switch switchEyeCloseEnable;
    Switch switchOcclusionMouthEnable;
    Switch switchOcclusionBrowEnable;
    Switch switchMultipleEnable;
    Switch switchMouthOpenEnable;
    Switch switchMaskEnable;
    EditText editThresholdDark;
    EditText editThresholdGlare;
    EditText editThresholdBlur;
    EditText editThresholdLiveness;
    EditText editSilentTimeOutMillis;
    EditText editMotionTimeOutMillis;
    EditText editReadyTimeOutMillis;
    EditText editMinDetectTimeMillis;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_threshold_setting,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        switchHeadPoseEnable = view.findViewById(R.id.switch_head_pose_enable);
        switchMaskEnable = view.findViewById(R.id.switch_mask_enable);
        switchOcclusionEyeEnable = view.findViewById(R.id.switch_occlusion_eye_enable);
        switchOcclusionMouthEnable = view.findViewById(R.id.switch_occlusion_mouth_enable);
        switchOcclusionBrowEnable = view.findViewById(R.id.switch_occlusion_brow_enable);
        editThresholdDark = view.findViewById(R.id.edit_threshold_bark);
        switchMouthOpenEnable = view.findViewById(R.id.switch_mouth_open_enable);
        editThresholdGlare = view.findViewById(R.id.edit_threshold_glare);
        switchMultipleEnable = view.findViewById(R.id.switch_multiple_enable);
        switchEyeCloseEnable = view.findViewById(R.id.switch_eye_close_enable);
        editReadyTimeOutMillis = view.findViewById(R.id.edit_ready_time_out_millis);
        editMotionTimeOutMillis = view.findViewById(R.id.edit_motion_time_out_millis);
        editSilentTimeOutMillis = view.findViewById(R.id.edit_silent_time_out_millis);
        editMinDetectTimeMillis = view.findViewById(R.id.edit_min_detect_time_millis);
        editThresholdLiveness = view.findViewById(R.id.edit_threshold_liveness);
        editThresholdBlur = view.findViewById(R.id.edit_threshold_blur);

        view.findViewById(R.id.btn_recover).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PreferenceUtil.setQualityConfig(new QualityConfig.Builder().build());
                PreferenceUtil.setLivenessConfig(new LivenessConfig.Builder().build());
                PreferenceUtil.setSilentConfig(new SilentConfig.Builder().build());
                PreferenceUtil.setMotionTimeOutMillis(new MotionConfig.Builder().build().motionTimeoutMillis);
                PreferenceUtil.setMotionReadyTimeOutMillis(new MotionConfig.Builder().build().readyTimeoutMillis);
                Toast.makeText(v.getContext(), "还原成功", Toast.LENGTH_SHORT).show();
            }
        });

        updateConfig();
    }
    private void updateConfig() {

        QualityConfig qualityConfig = PreferenceUtil.getQualityConfig();
        switchHeadPoseEnable.setChecked(qualityConfig.allowHeadTilted);
        switchMaskEnable.setChecked(qualityConfig.allowFaceMask);
        switchOcclusionEyeEnable.setChecked(qualityConfig.allowEyeOcclusion);
        switchEyeCloseEnable.setChecked(qualityConfig.allowEyeClose);
        switchOcclusionMouthEnable.setChecked(qualityConfig.allowMouthOcclusion);
        switchOcclusionBrowEnable.setChecked(qualityConfig.allowBrowOcclusion);
        switchMultipleEnable.setChecked(qualityConfig.allowMultiTarget);
        switchMouthOpenEnable.setChecked(qualityConfig.allowMouthOpen);
        editThresholdDark.setText(String.valueOf(qualityConfig.overDarkThreshold));
        editThresholdGlare.setText(String.valueOf(qualityConfig.overGlareThreshold));
        editThresholdBlur.setText(String.valueOf(qualityConfig.blurThreshold));


        LivenessConfig livenessConfig = PreferenceUtil.qetLivenessConfig();
        editThresholdLiveness.setText(String.valueOf(livenessConfig.livenessThreshold));

        SilentConfig silentConfig = PreferenceUtil.qetSilentConfig();
        editMinDetectTimeMillis.setText(String.valueOf(silentConfig.minDetectMillis));
        editSilentTimeOutMillis.setText(String.valueOf(silentConfig.timeoutMillis));

        editReadyTimeOutMillis.setText(String.valueOf(PreferenceUtil.getMotionReadyTimeOutMillis()));
        editMotionTimeOutMillis.setText(String.valueOf(PreferenceUtil.getMotionTimeOutMillis()));
    }

    @Override
    public void onPause() {
        super.onPause();
        PreferenceUtil.setQualityConfig(new QualityConfig.Builder()
                .setAllowEyeClose(switchEyeCloseEnable.isChecked())
                .setAllowHeadTilted(switchHeadPoseEnable.isChecked())
                .setAllowBrowOcclusion(switchOcclusionBrowEnable.isChecked())
                .setAllowMouthOcclusion(switchOcclusionMouthEnable.isChecked())
                .setAllowEyeOcclusion(switchOcclusionEyeEnable.isChecked())
                .setAllowMultiTarget(switchMultipleEnable.isChecked())
                .setAllowMouthOpen(switchMouthOpenEnable.isChecked())
                .setAllowFaceMask(switchMaskEnable.isChecked())
                .setOverDarkThreshold(Float.valueOf(editThresholdDark.getText().toString()))
                .setOverGlareThreshold(Float.valueOf(editThresholdGlare.getText().toString()))
                .setBlurThreshold(Float.valueOf(editThresholdBlur.getText().toString()))
                .build());
        PreferenceUtil.setLivenessConfig(new LivenessConfig.Builder()
                .setLivenessThreshold(Float.valueOf(editThresholdLiveness.getText().toString()))
                .build());
        PreferenceUtil.setSilentConfig(new SilentConfig.Builder()
                .setMinDurationMillis(Long.valueOf(editMinDetectTimeMillis.getText().toString()))
                .setTimeoutMillis(Long.valueOf(editSilentTimeOutMillis.getText().toString()))
                .build());
        PreferenceUtil.setMotionTimeOutMillis(Long.valueOf(editMotionTimeOutMillis.getText().toString()));
        PreferenceUtil.setMotionReadyTimeOutMillis(Long.valueOf(editReadyTimeOutMillis.getText().toString()));
        Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show();
    }
}